#!/usr/bin/python
# -*- coding: utf-8 -*-

from copy import copy

total_h=0
total_s=0


def clustering2(player_reputations):
	"""
	This function apply a simple K-mean clustering to the player's reputation in order to divide them into two group.
	The two calculated means are returned.
	"""
	#treat exeption
	if len(player_reputations)==0:
		#should not occur, but to be consistent, return valids goals.
		return [1,1]
	if len(player_reputations)==1:
		#if I play only against one player, I'll try to imitate him, so my goal should be his reputation
		return [player_reputations[0],player_reputations[0]]
	
	
		
	reputations=copy(player_reputations)#make a copy of the reputation to manipulate
	reputations.sort()
	
	mu=[-0.1,1.1]#dummy initialisation that garantie the good execution of the first loop
	new_mu=[reputations[0],reputations[-1]]#initialised centroids with the two extreme.
	
	#error case handle
	if new_mu[0]==new_mu[1]:
		#I think only occurs if every reputation are equal
		return [new_mu[0],new_mu[1]]
	
	#print new_mu
	while mu!=new_mu:
		mu=new_mu
		index=list()
		for reputation in reputations:
			dist=[abs(reputation-mu[0]),abs(reputation-mu[1])]
			if dist[0]<dist[1]: 
				#closer to mu[0]. so index assigned =0
				index.append(0)
			else:
				#closer to mu[1]
				index.append(1)
		
		#compute new mu
		total=[0,0]
		n=[0,0]#number of players in each set
		for i in range(len(reputations)):
			total[index[i]]+=reputations[i]
			n[index[i]]+=1
		new_mu=[float(total[0])/n[0],float(total[1])/n[1]]	
		
	return new_mu
	
def hunt_choices(round_number, current_food, current_reputation, m,  player_reputations):
	player_number=len(player_reputations)
	hunt_decisions=list()	
	
	
	if round_number==1:
		#for the first round, I cannot rely to the reputation of the player to get a mean as a goal.
		#So I just define it myself.
		desire_reputation=1
		
	else: #common case
		#I divise people into 2 set, based on their réputation, with a simple K-mean clustering.
		#the best reputation's set is considered as the trustworthy set.
		#my goal is to always be in this set. So I'll always define my reputation goal to the mean of this set.
		means=clustering2(player_reputations)
		desire_reputation=means[1]
	
	#retrieve the number of hunt & slacks I made
	global total_h
	global total_s
	
	#now that I know my desire_reputation at the end of this round, I compute the number of hunt I should go
	hunt_to_go_this_round=int(round(desire_reputation*(total_h+total_s+player_number)-total_h))
	
	#I will spend those hunt, with the player with the biggest reputation. So I need to find them.
	reputations_sorted=copy(player_reputations)
	reputations_sorted.sort()
	hunt_with=reputations_sorted[-hunt_to_go_this_round:] #save the hunt_to_go_this_round best reputation
	
	#finally, create the answer vector, in order
	for reputation in player_reputations:
		if reputation in hunt_with:
			hunt_decisions.append('h')
			hunt_with.remove(reputation)
			total_h+=1
		else:
			hunt_decisions.append('s')
			total_s+=1
	
	return hunt_decisions

def hunt_outcomes(food_earnings):
	#nothing to do
	pass 

def round_end(award, m, number_hunters):
	#nothing to do
	#could possibly used to determine the groups...
	pass
